//
//  HomeViewController.swift
//  LZFPhotoBrowser
//
//  Created by 梁梓烽 on 16/7/23.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private   var i = 1

class HomeViewController: UICollectionViewController {
   
    //懒加载放Shop模型的数组
    private lazy var shops :[Shop] = [Shop]()
    //创建动画对象
    private lazy var  photoBrowserAnimator = PhotoBrowserAnimator()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 调用网络工具类加载数据
        loadData(0)
       
    }
  
}

// MARK: - 请求数据
extension HomeViewController {
    
    //请求首页数据
    func loadData(offSet:Int){
        
        NetWorkTools.shareInstance.loadHomeData(offSet) { (result, error) -> () in
            //1.错误校验
            if error != nil
            {
                print(error)
                return
            }
            
            //2.拿到返回的字典数组
            guard let  resultArray = result else {
                //空
                print("出错了")
                return
            }
            
            //3.来到这里,说明resultArray已经拿到了并且有值,字典数组->模型数组
            for dic in resultArray{
                let shop = Shop(Dic: dic)
                self.shops.append(shop)
            }
            
            //4.刷新collectionview
            self.collectionView?.reloadData()
            
        }
        
    }
}

// MARK: - modal出图片浏览器控制器
extension HomeViewController{
    func  showBrowserController(indexPath:NSIndexPath){
        //1.创建控制器
        let browserController = PhotoBrowserController()
        
        //2.给browserController传值
        browserController.shops=self.shops
        browserController.indexPath = indexPath
        
        //.设置modal的样式
    
        browserController.modalPresentationStyle = .Custom
        browserController.transitioningDelegate = photoBrowserAnimator
        
        
        photoBrowserAnimator.indexPath=indexPath
        photoBrowserAnimator.presentDelegate = self
        photoBrowserAnimator.dismissDelegate = browserController
        
        
        //.modal出控制器
        presentViewController(browserController, animated: true, completion: nil)
    }
}

// MARK: - 实现 presentDelegate的方法
extension HomeViewController:PresentDelegate
{
    //返回选中cellde frame
    func homeRect(indexPath:NSIndexPath)->CGRect{
        //拿到选中的cell
        let selectedCell =  collectionView?.cellForItemAtIndexPath(indexPath) as! HomeViewCell
        
        //从collectionVIew的坐标转换成uiweindow中的坐标
        
        let homeRect =  collectionView?.convertRect(selectedCell.frame, toCoordinateSpace: UIApplication.sharedApplication().keyWindow!)
        
        return homeRect!
        
    }
    func photoBrowserRect(indexPath:NSIndexPath)->CGRect{
        //拿到选中的cell
        let selectedCell =  collectionView?.cellForItemAtIndexPath(indexPath) as! HomeViewCell

        return calculateFrame(selectedCell.imaegView.image!)
        
    }
    
    
    func imageView(indexPath : NSIndexPath) -> UIImageView{
        let imageView = UIImageView()
        
         let selectedCell =  collectionView?.cellForItemAtIndexPath(indexPath) as! HomeViewCell
        
        imageView.image = selectedCell.imaegView.image!
        
        imageView.clipsToBounds = true
        imageView.contentMode = .ScaleAspectFit
        
        return imageView
    }

}








// MARK: -HomeViewController的代理和数据源方法
extension HomeViewController {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        //print(self.shops.count)
        return self.shops.count
        
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HomeViewCell
             
        cell.shop = shops[indexPath.item]
         //cell.backgroundColor = UIColor.redColor()
        
        //判断加载的是最后一个cell的时候,重写发送请求加载更多数据
        if indexPath.item == (self.shops.count-1)
        {
            //加载更多的数据
         
            loadData(30*i)
            i++
        }
        
        return cell
    }
    override func  collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        

        showBrowserController(indexPath)
        
    }
    
    
}



